package com.czy.owner.ui.seckill;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.czy.owner.R;
import com.czy.owner.adapter.SeckillAdapter;
import com.czy.owner.api.SecondsKillListApi;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.OnLineOrderServiceModel;
import com.czy.owner.entity.OrdersListModel;
import com.czy.owner.entity.SeckillModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseFragment;
import com.czy.owner.ui.hotshop.GoodsDetailActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.BindView;

/**
 * Created by asus on 2017/5/17.
 */
public class SeckillFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
    private static final int SEC_KILL_TYPE = 1;
    @BindView(R.id.rv_seckill)
    EasyRecyclerView rvSeckill;
    private SeckillAdapter adapter;
    private Handler handler = new Handler();
    private int PLATFORM_SEC_TYPE = 0;
    private int STORE_SEC_TYPE = 1;


    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_seckill;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        rvSeckill.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvSeckill.addItemDecoration(new RecycleViewDivider(getActivity(), LinearLayoutManager.VERTICAL, R.drawable.shape_recyclerview_divider_decoration));
        rvSeckill.setAdapterWithProgress(adapter = new SeckillAdapter(getActivity(), getArguments().getInt("type", 0)));
        adapter.setError(R.layout.view_error_net, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();

            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();

            }
        });
        rvSeckill.setRefreshListener(this);


        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener<SeckillModel>() {
            @Override
            public void onItemClick(View view, int position, SeckillModel model) {
                if (PhoneUtils.isNetworkConnected(getActivity())) {
                    Intent ti = new Intent(getActivity(), GoodsDetailActivity.class);
                    ti.putExtra("fromType", SEC_KILL_TYPE);
                    ti.putExtra("specGroupKey", model.getSpecGroupKey());
                    ti.putExtra("price", model.getKillPrice());
                    ti.putExtra("mFromType", Constants.GOODSSOURCE_SECKILL);
                    ti.putExtra("mFromTypeId",  model.getGoodsActivityId() + "");
                    MyLog.e("yang","秒杀id=="+ model.getGoodsActivityId());
                    startActivity(ti);
                }

            }
        });

    }

    @Override
    protected void updateViews() {
        onRefresh();
    }


    private void getDataList() {
        SecondsKillListApi secondsKillListApi = new SecondsKillListApi(new HttpOnNextListener<List<SeckillModel>>() {
            @Override
            public void onNext(List<SeckillModel> models) {
                setDataList(models);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                PhoneUtils.ShowToastMessage(getActivity(), e.getMessage());
            }
        }, (RxAppCompatActivity) (getContext()));
        secondsKillListApi.setSession(UserHelper.getInstance().getSession(getActivity()));
        secondsKillListApi.setStoreId(SharedPreferencesUtils.getParam(getActivity(), UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
        Bundle arguments = getArguments();
        secondsKillListApi.setTime(arguments.getLong("time", 0) + "");
        if (arguments.getInt("fromType", PLATFORM_SEC_TYPE) == PLATFORM_SEC_TYPE) {
            secondsKillListApi.setBelongsType("system");
            secondsKillListApi.setBelongsTypeId("0");
        } else {
            secondsKillListApi.setBelongsType("store");
            secondsKillListApi.setBelongsTypeId(SharedPreferencesUtils.getParam(getActivity(), UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
        }

        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(secondsKillListApi);
    }


    private void setDataList(List<SeckillModel> models) {
        if (models.size() == 0) {
            rvSeckill.setEmptyView(R.layout.view_empty_common);
            rvSeckill.showEmpty();
        }
        if (models.size() == 0 && !PhoneUtils.isNetworkConnected(getActivity())) {
            rvSeckill.showError();
        } else {
            adapter.addAll(models);
        }
    }


    @Override
    public void onRefresh() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新
                adapter.clear();
                getDataList();
            }
        }, 1000);
    }


}
